<template>
  <!-- 法催案件管理-回收案件池-页面 -->
  <section class="page legal-recover-case-manage">
    <data-form :rules="rules" :model="model" @onSearch="refreshData" @handleResetForm="handleResetForm" :page="pageService">
      <template slot="inputs">
        <el-form-item label="订单编号" prop="caseNumber">
          <el-input v-model="model.caseNumber"></el-input>
        </el-form-item>
        <el-form-item label="批次号" prop="batchNumber">
          <el-input v-model="model.batchNumber"></el-input>
        </el-form-item>
        <el-form-item label="委托方" prop="principalId">
          <el-select v-model="model.principalId" placeholder="请选择" clearable>
            <el-option v-for="{index,name,id} in principalList.content" :key="index" :label="name" :value="id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="客户姓名" prop="personalName">
          <el-input v-model="model.personalName"></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="idCard">
          <el-input v-model="model.idCard"></el-input>
        </el-form-item>
        <el-form-item label="放款日期" prop="loanDate">
          <el-date-picker type="daterange" :editable="false" v-model="model.loanDate" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="手机号" prop="mobileNo">
          <el-input v-model="model.mobileNo"></el-input>
        </el-form-item>
        <el-form-item label="业务团队" prop="partnerName">
          <el-input v-model="model.partnerName"></el-input>
        </el-form-item>
        <!-- <el-form-item label="机构" prop="deptIds">
          <select-department v-model="model.deptId" @on-select-change="(id, code) => departemntChange(id, code)"></select-department>
        </el-form-item>
        <el-form-item label="催收员" prop="currentCollectorRealName">
          <el-input v-model="model.currentCollectorRealName" clearable></el-input>
        </el-form-item> -->
        <el-form-item label="部门催收剩余天数" prop="partCollectionRemainingDays">
          <number-range v-model="model.partCollectionRemainingDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="个人催收剩余天数" prop="personalCollectionRemainingDays">
          <number-range v-model="model.personalCollectionRemainingDays" :isInteger="true"></number-range>
        </el-form-item>
        <el-form-item label="贷款银行" prop="bankName">
          <el-input v-model="model.bankName"></el-input>
        </el-form-item>
        <el-form-item label="催收总金额" prop="overdueAmount">
          <number-range v-model="model.overdueAmount"></number-range>
        </el-form-item>
        <el-form-item label="业务承接地" prop="businessAddress">
          <el-input v-model="model.businessAddress"></el-input>
        </el-form-item>
        <el-form-item label="上牌地" prop="boardAddress">
          <el-input v-model="model.boardAddress"></el-input>
        </el-form-item>
        <el-form-item label="车牌号" prop="carNo">
          <el-input v-model="model.carNo"></el-input>
        </el-form-item>
        <el-form-item label="案件来源" prop="dataFromSource">
          <el-select v-model="model.dataFromSource" placeholder="请选择" clearable>
            <el-option v-for="{index,name,code} in $dict.getDictData('DataFromSource')" :key="index" :label="name" :value="code"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="案件流入时间" prop="followInTime">
          <el-date-picker type="datetimerange" :editable="false" v-model="model.followInTime" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
        <el-form-item label="个人回收时间" prop="recordFollowInTime">
          <el-date-picker type="datetimerange" :editable="false" v-model="model.recordFollowInTime" placeholder="选择日期时间" clearable>
          </el-date-picker>
        </el-form-item>
      </template>
    </data-form>

    <data-box :data="dataSet" :selectionList.sync="selectionList" @onPageChange="refreshData" :sort="sortService" :page="pageService" ref="data-box">
      <template slot="buttons">
        <el-button @click="moveToWaitDistribution" v-auth="314">移入法务待分配</el-button>
        <el-button @click="advanceCirculation" v-auth="315">申请流转</el-button>
        <el-button @click="dealManualPartReturnCase" v-auth="275">委托方回收</el-button>
      </template>
      <template slot="columns">
        <el-table-column prop="caseNumber" label="订单编号" :min-width="$helper.getColumnWidth(6)">
          <template slot-scope="scope">
            <el-button type="text" @click="openExecutionPage(scope.row)">{{scope.row.caseNumber}}</el-button>
          </template>
        </el-table-column>
        <el-table-column prop="batchNumber" label="批次号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="principalName" label="委托方" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="personalName" label="客户姓名" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="idCard" label="身份证号" :min-width="$helper.getColumnWidth(6)">
        </el-table-column>
        <el-table-column prop="loanDate" label="放款日期" :formatter="(row) => $filter.dateFormat(row.loanDate)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="mobileNo" label="手机号" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="partnerName" label="业务团队" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="bankName" label="贷款银行" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="partCollectionRemainingDays" label="部门催收剩余天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="personalCollectionRemainingDays" label="个人催收剩余天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <!-- <el-table-column prop="deptName" label="机构" show-overflow-tooltip :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="currentCollectorRealName" label="催收员" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="leaveCaseFlag" label="留案标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.leaveCaseFlag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="leaveDueDate" label="留案到期日期" :min-width="$helper.getColumnWidth(5)" :formatter="(row) => $filter.dateFormat(row.leaveDueDate)">
        </el-table-column>
        <el-table-column prop="telAssistflag" label="电催协催标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.telAssistflag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="assistFlag" label="外访协催标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.assistFlag,'CommonFlag')">
        </el-table-column> -->
        <el-table-column prop="riskFlag" label="风险客户标识" :min-width="$helper.getColumnWidth(3)" :formatter="(row) => $filter.dictConvert(row.riskFlag,'CommonFlag')">
        </el-table-column>
        <el-table-column prop="loanAmount" label="贷款金额" :min-width="$helper.getColumnWidth(4)" :formatter="(row) =>  $filter.toThousands(row.loanAmount)">
        </el-table-column>
        <el-table-column prop="loanPeriod" label="贷款期数" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overdueStage" label="逾期阶段" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="overdueDays" label="逾期天数" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="sumMoney" label="催收总金额" :min-width="$helper.getColumnWidth(6)" :formatter="(row) =>  $filter.toThousands(row.overdueAmount)">
        </el-table-column>
        <el-table-column prop="businessAddress" label="业务承接地" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="boardAddress" label="上牌地" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="carNo" label="车牌号" :min-width="$helper.getColumnWidth(3)">
        </el-table-column>
        <el-table-column prop="dataFromSource" label="案件来源" :formatter="(row) => $filter.dictConvert(row.dataFromSource,'DataFromSource')" :min-width="$helper.getColumnWidth(4)">
        </el-table-column>
        <el-table-column prop="followInTime" label="案件流入时间" :formatter="(row) => $filter.dateTimeFormat(row.followInTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
        <el-table-column prop="recordFollowInTime" label="个人回收时间" :formatter="(row) => $filter.dateTimeFormat(row.recordFollowInTime)" :min-width="$helper.getColumnWidth(5)">
        </el-table-column>
      </template>
    </data-box>

    <!--申请弹框-->
    <el-dialog :title="`${applyType.name}申请`" width="800px" :visible.sync="dialog.apply" :show-close="false" :close-on-click-modal="false" :close-on-press-escape="false">
      <apply :caseIds="selectedCaseIds" :fromChannel="caseChannel" :applyType="applyType.value" @close="dialog.apply = false;refreshData()" ref="apply-circulation"></apply>
    </el-dialog>

    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :show-close="false" title="委托方回收" :visible.sync="dialog.principalRecovery" width="400px">
      <principal-recovery :ids="selectedCaseIds" @close="dialog.principalRecovery = false;refreshData();" />
    </el-dialog>
  </section>
</template>

<script lang="ts">
import Vue from 'vue';
import Component from 'vue-class-component';
import { Dependencies, Layout, Auth } from '~/core/decorator';
import { Getter, namespace, State } from 'vuex-class';
import { PageService } from '~/utils/page.service';
import { SortService } from '~/utils/sort.service';
import SelectDepartment from "~/components/business-component/select-department.vue";

import NumberRange from '~/components/common/number-range.vue';
import { LegalCaseManageService } from '~/services/domain-service/legal-case-manage.service';
import Apply from '~/components/visit-case/apply.vue';
import { ApplyType, CASE_CHANNEL, MENU_CHANNEL } from '~/config/enum.config';
import GetLabelName from "~/components/labeling-strategy/get-label-name.vue";
import PrincipalRecovery from "~/components/apply-component/principal-recovery.vue";

const CollectionExecutionModule = namespace('collection-execution');

@Auth(306)
@Layout('workspace')
@Component({
  components: {
    NumberRange,
    Apply,
    GetLabelName,
    SelectDepartment,
    PrincipalRecovery
  },
})
export default class LegalRecoverCaseManage extends Vue {
  @Dependencies(PageService)
  private pageService: PageService;
  @Dependencies(SortService)
  private sortService: SortService;
  @Dependencies(LegalCaseManageService)
  private legallyCaseManageServiceTestE: LegalCaseManageService;
  @Getter private callDepartmentData;
  @State private principalList: any;

  private model: any = {
    caseNumber: "",
    personalName: "",
    mobileNo: "",
    idCard: "",
    overdueAmount: {
      max: "",
      min: ""
    },
    loanDate: [],
    principalId: '',
    partCollectionRemainingDays: { min: '', max: '' },
    personalCollectionRemainingDays: { min: '', max: '' },
    bankName: '',
    businessAddress: '',
    boardAddress: '',
    carNo: '',
    dataFromSource: '',
    followInTime: [],
    deptId: '',
    deptIds: [],
    currentCollectorRealName: '',
    recordFollowInTime: [],
    partnerName: '',
    batchNumber: ''
  };
  private rules: any = {};
  private dataSet: any[] = [];
  private selectionList: any[] = [];
  private dialog: any = {
    apply: false,
    principalRecovery: false
  };
  private caseChannel = CASE_CHANNEL.legal;
  private applyType: any = {};

  private created() {
    this.refreshData();
  }

  private refreshData() {
    this.legallyCaseManageServiceTestE
      .recoverCasePool(this.paramsHandler(), this.pageService, this.sortService)
      .subscribe((data) => {
        this.dataSet = data
        this.selectionList = []
      });
  }

  private departemntChange(id, code) { }


  private paramsHandler() {
    const params = { ...this.model }
    params.deptId = this.model.deptIds.length ? this.model.deptIds[this.model.deptIds.length - 1] : ''
    params.overdueAmountMax = this.model.overdueAmount ? this.model.overdueAmount.max : ""
    params.overdueAmountMin = this.model.overdueAmount ? this.model.overdueAmount.min : ""
    params.partCollectionRemainingDaysMax = params.partCollectionRemainingDays ? this.model.partCollectionRemainingDays.max : ""
    params.partCollectionRemainingDaysMin = params.partCollectionRemainingDays ? this.model.partCollectionRemainingDays.min : ""
    params.personalCollectionRemainingDaysMax = params.personalCollectionRemainingDays ? this.model.personalCollectionRemainingDays.max : ""
    params.personalCollectionRemainingDaysMin = params.personalCollectionRemainingDays ? this.model.personalCollectionRemainingDays.min : ""
    params.followInTimeMin = this.model.followInTime.length ? this.$filter.dateTimeFormat(this.model.followInTime[0]) : ''
    params.followInTimeMax = this.model.followInTime.length ? this.$filter.dateTimeFormat(this.model.followInTime[1]) : ''
    params.loanDateMin = this.$filter.dateRanageFormat(this.model.loanDate).start
    params.loanDateMax = this.$filter.dateRanageFormat(this.model.loanDate).end
    params.recordFollowInTimeMin = this.model.recordFollowInTime.length ? this.$filter.dateTimeFormat(this.model.recordFollowInTime[0]) : ''
    params.recordFollowInTimeMax = this.model.recordFollowInTime.length ? this.$filter.dateTimeFormat(this.model.recordFollowInTime[1]) : ''

    delete params.overdueAmount
    delete params.partCollectionRemainingDays
    delete params.personalCollectionDays
    delete params.followInTime
    delete params.loanDate
    delete params.recordFollowInTime
    delete params.deptIds

    return params
  }

  /**
   * 表单范围输入框重置
   */
  private handleResetForm() {
    this.model.overdueAmount = { min: "", max: "" };
    this.model.partCollectionRemainingDays = { min: "", max: "" };
    this.model.personalCollectionRemainingDays = { min: "", max: "" };
  }

  /**
   * 提前流转
   */
  private advanceCirculation() {
    if (this.selectionList.length === 0) {
      this.$message.info('请选择要流转的案件');
      return;
    }
    this.applyType = ApplyType.turn
    this.dialog.apply = true;
  }

  /**
   *
   */
  private moveToWaitDistribution() {
    if (this.selectionList.length === 0) {
      this.$message.info('请选择要回收的案件');
      return;
    }
    this.$confirm(`您确定要将已选择的案件移入电催待分配吗?`, '提示', {
      confirmButtonText: '确定',
      cancelButtonText: '取消',
      type: 'warning',
    }).then(() => {
      this.legallyCaseManageServiceTestE
        .moveToWaitDistribution({
          ids: this.selectionList.map((v) => v.id),
        })
        .subscribe(
          (data) => {
            this.$message.success('操作成功');
            this.refreshData();
          }
        );
    });
  }

  private get selectedCaseIds() {
    return this.selectionList.map((v) => v.id);
  }

  /**
   * 进入查看执行页
   */
  private openExecutionPage(row) {
    this.$router.openCaseDetail(row.id, {
      menuChannel: MENU_CHANNEL.Legal_recover,
      hiddenCaseList: true,
      hiddenAction: true
    });
  }
  /**
   * 委托方回收
   */
  private dealManualPartReturnCase() {
    if (this.selectionList.length !== 0) {
      this.dialog.principalRecovery = true;
    } else {
      this.$message(`请选择要进行委托方回收的案件`);
    }
  }
}
</script>

<style lang="less" scoped>
</style>
